import java.util.HashMap;
import java.util.Set;


/**
 * @author yexiebao
 * @date 2025/5/17
 */
public class IsAnagram {
    //复杂
    public static boolean isAnagram(String s, String t) {
        //1.长度不相等，肯定不是
        if (s.length() != t.length()) {
            return false;
        }
        //2.创建一个HashMap
        HashMap<Character,Integer> visitedMap = new HashMap<>();
        for(int i = 0; i< s.length();i++){
            visitedMap.put(s.charAt(i),visitedMap.getOrDefault(s.charAt(i),0)+1);
        }
        for(int i = 0; i< t.length();i++){
            if(visitedMap.containsKey(t.charAt(i))){
                visitedMap.put(t.charAt(i),visitedMap.getOrDefault(t.charAt(i),0)-1);
            }else{
                return false;
            }
        }
        for (Integer value : visitedMap.values()){
            if(value != 0){
                return false;
            }
        }
        return true;
    }

    //简便
    public static boolean isAnagramSimple(String s, String t){
        if(s.length()!=t.length()){
            return false;
        }
        HashMap<Character, Integer>visited = new HashMap<>();
        for(char c : s.toCharArray()){
            visited.put(c,visited.getOrDefault(c,0)+1);
        }
        for(char c : t.toCharArray()){
            if(!visited.containsKey(c)){
                return false;
            }
            visited.put(c,visited.get(c)-1);
            if(visited.get(c)<0){
                return false;
            }
        }
        return true;
    }
}